package com.summer.safe.controller.system;

import com.summer.safe.base.BaseController;
import com.summer.safe.base.Principal;
import com.summer.safe.utils.Global;
import com.summer.safe.utils.SessionUtils;
import com.summer.safe.utils.ShiroUtils;
import com.summer.safe.utils.UserUtils;
import com.summer.safe.utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录
 */
@Controller
public class LoginController extends BaseController {

    /**
     * 登录页面
     * @return
     */
    @GetMapping("/login")
    public String login() {
        return "login.html";
    }

    /**
     * 验证登录
     * @param
     * @return
     */

    @PostMapping(value = "/login")
    public String loginFail(Model model, HttpServletRequest request, HttpServletResponse response) {
        Principal principal = ShiroUtils.getPrincipal();
        // 如果已经登录，则跳转到管理首页
        if (principal != null) {
            return REDIRECT+"/index";
        }else {
            SessionUtils.removeSessionAttribute(SessionUtils.SKEY_USER_ID);
            String message = (String)request.getAttribute(Global.DEFAULT_MESSAGE_PARAM);
            model.addAttribute(Global.DEFAULT_MESSAGE_PARAM, message);
            return "login.html";
        }

    }

    /**
     * 退出登陆
     * @return
     */
    @GetMapping(value = "/logout")
    public String logOut() {
        UserUtils.clearCache();
        //退出
        ShiroUtils.logout();
        return REDIRECT+"/login";
    }



}
